package lzupdater.locale;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * A String that takes care of locale. The value returns by toString() is always
 * uptodate with the language used by the dictionary.
 * 
 * @author Fraskor
 */
public class LocalizedString implements LocaleListener {

	/** The key in the dictionary */
	private final String key;
	/** the localized value associated to the key */
	private String value;

	/**
	 * Create the localized string with the key 'key'
	 */
	public LocalizedString(String key) {
		this.key = key;
		value = key;
		Dictionary.addLocaleListener(this);
	}

	/**
	 * {@inheritDoc}
	 */
	public void localeChange(Locale locale) {
		value = Dictionary.translate(locale, key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return value;
	}

	/**
	 * Use the localizedString as formater, i.e. str.useAsFormater(...args) is
	 * equivalent to String.format(str.toString(),...args)
	 * 
	 * @param args
	 * @return a formated string accordingly to the value of this
	 *         localizedString
	 */
	public String useAsFormater(Object... args) {
		return String.format(value, args);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getKey() {
		return key;
	}

}
